package com.zhuge_new.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FadeExit.FadeExit;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.maning.mndialoglibrary.MToast;
import com.zhuge_new.Activity.CardExamineActivity;
import com.zhuge_new.Activity.CreditCardActivity;
import com.zhuge_new.Adapter.CardExamineAdapter;
import com.zhuge_new.R;
import com.zhuge_new.bean.ExamBean;
import com.zhuge_new.http.StringDialogCallback;
import com.zhuge_new.share.BaseHttp;
import com.zhuge_new.untils.DataParse;
import com.zhuge_new.untils.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by lenovo on 2018-09-15.
 */

public class CardFragment extends Fragment {

    private ExamBean examBean;
    private RecyclerView listview;
    private View footerView;
    protected boolean isInit = false;
    private CardExamineAdapter cardExamineAdapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.kabaofragment, container, false);
        footerView = View.inflate(getContext(), R.layout.foor_card, null);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isInit=true;
        init_title();
        getData();
    }

    private void init_title() {
        listview = getActivity().findViewById(R.id.relist);
    }

    public void getData() {
        OkGo.<String>post(BaseHttp.creditcard_datas)
                .tag(this)
                .headers("token", PreferencesUtils.getString(getActivity(), "token"))
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj=new JSONObject(response.body());
                            if (obj.getString("creditcards").equals("")){

                            }else {
                                examBean = (ExamBean) new DataParse().getBean(response.body(), ExamBean.class, 0);
                                cardExamineAdapter = new CardExamineAdapter(getActivity(), examBean);
                                listview.setLayoutManager(new LinearLayoutManager(getActivity()));
                                cardExamineAdapter.addFooterView(footerView);
                                cardExamineAdapter.setHeaderFooterEmpty(false, true);
                                listview.setAdapter(cardExamineAdapter);
                                cardExamineAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                        if (view.getId() == R.id.card_delete){
                                            card_delete(examBean.getCreditcards().get(position).getCreditcardId(),position);
                                        }
                                    }
                                });
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });


    }

    private void card_delete(final String creditcardId, final int position) {
        BaseAnimatorSet bas_in = new FlipVerticalSwingEnter();
        BaseAnimatorSet bas_out = new FadeExit();
        final NormalDialog dialog = new NormalDialog(getContext());
        dialog.content("您确定要删除这张信用卡吗")
                .style(NormalDialog.STYLE_TWO)
                .titleTextSize(23)
                .showAnim(bas_in)
                .dismissAnim(bas_out)
                .show();
        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                        delete(creditcardId,position);
                    }
                });
    }

    private void delete(String creditcardId, final int position) {
        OkGo.<String>post(BaseHttp.creditcard_del_sub)
                .tag(this)
                .headers("token", PreferencesUtils.getString(getActivity(),"token"))
                .params("creditcardId",creditcardId)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccessResponse(Response<String> response, String msg, String msgCode) {
                        try {
                            JSONObject obj=new JSONObject(response.body());
                            MToast.makeTextShort(getActivity(),obj.getString("msg")).show();
                            cardExamineAdapter.remove(position);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

}
